package com.coinworld.response;

import com.fasterxml.jackson.annotation.JsonFormat;

public class ResponseCode {


    @JsonFormat(shape = JsonFormat.Shape.OBJECT)
    public enum Common implements IResponseCode {

        /* 返回Code码 */
        SUCCESS(0, "成功"),
        FAILURE(-1, "失败"),
        INVALID_PARAMETER(400, "参数非法"),
        UNAUTHORIZED(401, "无权访问"),
        INTERFACE_EXCEPTION(402, "调用接口异常"),
        LOGIN_INVALID(405, "请重新登录"),
        FREQUENT_OPERATIONS(406, "操作频繁");


        private int code;
        private String msg;

        Common(int code, String msg) {
            this.code = code;
            this.msg = msg;
        }

        @Override
        public int getCode() {
            return code;
        }

        @Override
        public String getMsg() {
            return msg;
        }

        public static String getName(int index) {
            for (Common common : Common.values()) {
                if (common.code == index) {
                    return common.msg;
                }
            }
            return null;
        }

        public static Integer getIndex(String name) {
            for (Common common : Common.values()) {
                if (common.msg.equals(name)) {
                    return common.code;
                }
            }
            return null;
        }

    }


    public interface IResponseCode {

        int getCode();

        String getMsg();

    }
}
